package com.rmxx.fruit.fragment;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.company.android.fruit.R;
import com.kwan.xframe.common.widget.indicator.GridSpacingItemDecoration;
import com.kwan.xframe.common.widget.observablescrollview.ObservableRecyclerView;
import com.kwan.xframe.mvp.view.fragment.BasePageItemFragment;
import com.rmxx.fruit.activity.ProductActivity;
import com.rmxx.fruit.activity.ProductListActivity;
import com.rmxx.fruit.bean.Product;

import java.util.ArrayList;
import java.util.List;


public class HomeFragment extends BasePageItemFragment {

	ObservableRecyclerView rv_second, rv_fix,rv_product;
	List<Integer> mSecond = new ArrayList<>();
	List<Product> mFix = new ArrayList<>();
	List<Product> mProduct = new ArrayList<>();

	public HomeFragment() {
	}

	// TODO: Rename and change types and number of parameters
	public static HomeFragment newInstance() {
		HomeFragment fragment = new HomeFragment();
		Bundle args = new Bundle();
		fragment.setArguments(args);
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (getArguments() != null) {
		}

		rv_second = mBaseContentView.findViewById(R.id.rv_second);
		mSecond.add(R.mipmap.fruit01);
		mSecond.add(R.mipmap.fruit02);
		mSecond.add(R.mipmap.fruit03);
		mSecond.add(R.mipmap.fruit01);
		mSecond.add(R.mipmap.fruit02);
		mSecond.add(R.mipmap.fruit03);
		BaseQuickAdapter<Integer, BaseViewHolder> mSecondAdapter =
				new BaseQuickAdapter<Integer, BaseViewHolder>(R.layout.list_item_sec, mSecond) {
					@Override
					protected void convert(BaseViewHolder helper, Integer item) {
						helper.setImageResource(R.id.iv_icon, item);
					}
				};
		initRecycle(rv_second, mSecondAdapter,
				new LinearLayoutManager(getActivity(),
						LinearLayoutManager.HORIZONTAL, false));


		rv_fix = mBaseContentView.findViewById(R.id.rv_fix);
		Product product = new Product();
		product.setRicon(R.mipmap.product3_01);
		product.setName("莱家曲奇饼榛子味125g");
		mFix.add(product);

		product = new Product();
		product.setRicon(R.mipmap.product3_02);
		product.setName("莱家曲奇饼榛子味125g");
		mFix.add(product);

		product = new Product();
		product.setRicon(R.mipmap.product3_03);
		product.setName("莱家曲奇饼榛子味125g");
		mFix.add(product);
		BaseQuickAdapter<Product, BaseViewHolder> mFixAdapter =
				new BaseQuickAdapter<Product, BaseViewHolder>(R.layout.list_item_fix, mFix) {
					@Override
					protected void convert(BaseViewHolder helper, Product item) {
						helper.setImageResource(R.id.iv_icon, item.getRicon());
						helper.setText(R.id.tv_name,item.getName());
					}
				};
		initRecycle(rv_fix, mFixAdapter,
				new GridLayoutManager(getActivity(), 2,
						LinearLayoutManager.VERTICAL, false));
		int spanCount = 2;
		int spacing = 20;
		rv_fix.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacing, true));

		rv_product = mBaseContentView.findViewById(R.id.rv_product);
		product = new Product();
		product.setRicon(R.mipmap.product4_01);
		product.setName("【2袋】张君雅拉面丸子100g");
		product.setDesc("我的火辣不要轻易挑战哦");
		product.setPrice(6.66f);
		mProduct.add(product);

		product = new Product();
		product.setRicon(R.mipmap.product4_02);
		product.setName("真丽斯沐浴露持久留香");
		product.setDesc("洗澡就能护肤");
		product.setPrice(48.00f);
		mProduct.add(product);

		product = new Product();
		product.setRicon(R.mipmap.product4_03);
		product.setName("瑞松秋刀鱼即食海鲜类鱼干");
		product.setDesc("嚼劲十足 鲜香炭烤 深海捕捞");
		product.setPrice(48.00f);
		mProduct.add(product);

		BaseQuickAdapter<Product, BaseViewHolder> mProductAdapter =
				new BaseQuickAdapter<Product, BaseViewHolder>(R.layout.list_item_shop_product, mProduct) {
					@Override
					protected void convert(BaseViewHolder helper, Product item) {
						helper.setImageResource(R.id.iv_icon, item.getRicon());
						helper.setText(R.id.tv_name,item.getName());
						helper.setText(R.id.tv_price,"¥"+item.getPrice());
						helper.setText(R.id.tv_desc,item.getDesc());
					}
				};
		initRecycle(rv_product, mProductAdapter,
				new LinearLayoutManager(getActivity()));


		mContentView.findViewById(R.id.iv_product_list).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				go2Activity(ProductListActivity.class,null,false);
			}
		});

		mContentView.findViewById(R.id.iv_sec).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				go2Activity(ProductListActivity.class,null,false);
			}
		});

		mContentView.findViewById(R.id.iv_fix).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				go2Activity(ProductListActivity.class,null,false);
			}
		});
	}

	void initRecycle(RecyclerView mRecyclerView, BaseQuickAdapter mBaseAdapter, RecyclerView.LayoutManager layoutManager) {
		mRecyclerView.setLayoutManager(layoutManager);
		mRecyclerView.setItemAnimator(new DefaultItemAnimator());
		mRecyclerView.setHasFixedSize(true);
		mRecyclerView.setNestedScrollingEnabled(false);
		//int spanCount = 2;
		//int spacing = 20;
		//mRecyclerView.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacing, true));
		//麻痹的 关闭硬件加速 否则 cardView 报错
		mRecyclerView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		mRecyclerView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
		mRecyclerView.setAdapter(mBaseAdapter);

		mBaseAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
			@Override
			public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
				go2Activity(ProductActivity.class,null,false);
			}
		});
	}


	@Override
	protected int getContentViewId() {
		return R.layout.fragment_home;
	}

	@Override
	public void fetchData() {

	}

	@Override
	public String getPageName() {
		return null;
	}
}
